<template>
  <el-upload
    class="avatar-uploader"
    :show-file-list="false"
    action
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
    :http-request="uploadFile"
  >
    <span>
      <span v-if="imageUrl">
        <img :src="imageUrl" class="avatar" />
        <span class="removeAction" @click.stop="removeAction">
          <i class="el-icon-delete" />
        </span>
      </span>
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </span>
  </el-upload>
</template>
<script>
export default {
  props: ['label', 'name', 'value'],
  data() {
    return {
      imageUrl: '',
      uploadUrl: process.env.VUE_APP_BASE_API + '/PersonalCenter/UploadLocalFileAsync',
      datas: {
        base64Content: '',
        extension: ''
      },
      arr: [],
      file: null
    }
  },
  watch: {
    value(val) {
      if (this.value) {
        this.imageUrl = process.env.VUE_APP_BASE_API + this.value
      }
    }
  },
  created() {
    if (this.value) {
      this.imageUrl = process.env.VUE_APP_BASE_API + this.value
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      this.file = file
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      // console.log(this.datas)

      return isLt2M
    },
    uploadFile() {
      this.getBase64(this.file).then(resBase64 => {
        this.datas.base64Content = `data:image/${this.file.name.split('.')[1]};base64,` + resBase64.split(',')[1] // 直接拿到base64信息

        this.api.UploadLocalFileAsync(this.datas).then(res => {
          this.imageUrl = process.env.VUE_APP_BASE_API + res
          this.$emit('input', this.name, res)
        })
      })
    },
    removeAction() {
      this.imageUrl = null
    },
    getBase64(file) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader()
        let fileResult = ''
        reader.readAsDataURL(file)
        // 开始转
        reader.onload = () => {
          fileResult = reader.result
        }
        // 转 失败
        reader.onerror = e => {
          reject(e)
        }
        // 转 结束  咱就 resolve 出去
        reader.onloadend = () => {
          resolve(fileResult)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.avatar-uploader /deep/ .el-upload {
  position: relative !important;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #0486fe;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 1.5rem;
  height: 1.5rem;
  line-height: 1.5rem;
  text-align: center;
}
.avatar {
  width: 1.5rem;
  height: 1.5rem;
  display: block;
}
.removeAction {
  position: absolute;
  padding-top: 40%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  font-weight: bold;
}
</style>
